package com.innodealing.offsetskip;

import com.innodealing.offsetskip.aspect.KafkaOffsetSkipAspect;
import com.innodealing.offsetskip.config.KafkaOffsetSkipProperties;
import com.innodealing.offsetskip.handler.KafkaOffsetSkipHandler;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;

/**
 * Kafka Offset Skipper 自动配置类
 * 
 * 该配置类负责自动装配 Kafka 消息跳过功能所需的所有组件
 */
@Configuration
@EnableConfigurationProperties(KafkaOffsetSkipProperties.class)
@ConditionalOnClass({KafkaListener.class, Acknowledgment.class, ProceedingJoinPoint.class})
@ConditionalOnProperty(prefix = "kafka.offset-skip", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableAspectJAutoProxy
public class KafkaOffsetSkipAutoConfiguration {

    /**
     * 创建 Kafka Offset Skipper 处理器
     */
    @Bean
    @ConditionalOnMissingBean
    public KafkaOffsetSkipHandler kafkaOffsetSkipperHandler(KafkaOffsetSkipProperties properties) {
        return new KafkaOffsetSkipHandler(properties);
    }

    /**
     * 创建 Kafka Offset Skipper AOP 切面
     */
    @Bean
    @ConditionalOnMissingBean
    public KafkaOffsetSkipAspect kafkaOffsetSkipperAspect(KafkaOffsetSkipHandler handler) {
        return new KafkaOffsetSkipAspect(handler);
    }
}
